﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GingerBeard.Utilities
{
	public class Node
	{
		List<Node> nodes = new List<Node>();
		public List<Node> Nodes
		{
			get { return nodes; }
			set { nodes = value; }
		}

		public List<T> FindNodes<T>() where T : Node
		{
			List<T> res = new List<T>();
			foreach (Node n in Nodes)
			{
				FindInternal(false, n, res);
			}
			return res;
		}

		public List<T> FindNodesRecursive<T>() where T : Node
		{
			List<T> res = new List<T>();
			foreach (Node n in Nodes)
			{
				FindInternal(true, n, res);
			}
			return res;
		}

		private static void FindInternal<T>(bool recurse, Node cur, List<T> results) where T : Node
		{
			if (cur is T)
			{
				results.Add(cur as T);
			}

			if (recurse)
			{
				foreach (Node n in cur.Nodes)
				{
					FindInternal(recurse, n, results);
				}
			}
		}
	}
}
